<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="材料清单编号" prop="vBillNo">
        <el-input
          v-model="queryParams.vBillNo"
          placeholder="请输入材料清单编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提交人" prop="applyBy">
        <el-input
          v-model="queryParams.applyBy"
          placeholder="请输入提交人"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目编号" prop="vProjectNo">
        <el-input
          v-model="queryParams.vProjectNo"
          placeholder="请输入项目编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目描述" prop="vProjectDec">
        <el-input
          v-model="queryParams.vProjectDec"
          placeholder="请输入项目编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新建</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="spaList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="60" type="index"></el-table-column>
      <!-- <el-table-column label="角色编号" prop="roleId" width="120" /> -->
      <el-table-column
        label="材料清单编号"
        prop="vBillNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="项目编号"
        prop="vProjectNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="项目描述"
        prop="vProjectDec"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="提交人" prop="applyBy" width="100" />
      <el-table-column label="提交时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" width="100" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="view(scope.row)"
            >详情</el-button
          >

          <el-button size="mini" type="text" @click="exportData(scope.row)"
            >导出价格表</el-button
          >

          <!-- <el-button size="mini" type="text" @click="getZip()">打包</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-drawer
      title="详情"
      :visible.sync="drawerVisibie"
      size="80%"
      ref="drawer"
    >
      <div class="drawer-container">
        <div class="xin-main">
          <div class="xin-content" style="height: 90%">
            <el-form :model="drawerForm">
              <el-row>
                <el-col :span="6">
                  <el-form-item
                    label="材料清单编号"
                    :label-width="formLabelWidth"
                  >
                    <el-input
                      v-model="drawerForm.vBillNo"
                      autocomplete="off"
                      disabled
                    ></el-input> </el-form-item
                ></el-col>
                <el-col :span="6">
                  <el-form-item label="申请人" :label-width="formLabelWidth">
                    <el-input
                      v-model="drawerForm.applyBy"
                      autocomplete="off"
                    ></el-input> </el-form-item
                ></el-col>

                <el-col :span="6">
                  <el-form-item label="项目编号" :label-width="formLabelWidth">
                    <el-input
                      v-model="drawerForm.vProjectNo"
                      autocomplete="off"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="项目描述" :label-width="formLabelWidth">
                    <el-input
                      v-model="drawerForm.vProjectDec"
                      autocomplete="off"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <el-divider content-position="left"></el-divider>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  plain
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleTableAdd"
                  >新增</el-button
                >
              </el-col>

              <el-col :span="1.5">
                <el-button
                  type="primary"
                  plain
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleImport"
                  >导入</el-button
                >
              </el-col>
            </el-row>

            <el-table :data="drawerList" height="650px" border>
              <el-table-column
                type="index"
                label="序号"
                align="center"
                width="50"
              ></el-table-column>
              <el-table-column
                prop="vKind"
                label="类型"
                align="center"
                min-width="100"
              >
                <template slot-scope="scope">
                  <!-- <el-input
                    size="small"
                    v-model="scope.row.vKind"
                    @change="handleEdit(scope.$index, scope.row)"
                  >
                </el-input> -->
                  <el-select
                    size="small"
                    v-model="scope.row.vKind"
                    @change="handleEdit(scope.$index, scope.row)"
                  >
                    <el-option
                      v-for="dict in dict.type.spa_spa_kind"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column
                prop="vSpaName"
                label="零件名称"
                align="center"
                min-width="100"
              >
                <template slot-scope="scope">
                  <el-input
                    size="small"
                    v-model="scope.row.vSpaName"
                    @change="handleEditPrice(scope.$index, scope.row)"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="vSpaModel"
                label="规格型号"
                align="center"
                min-width="100"
              >
                <template slot-scope="scope">
                  <el-input
                    size="small"
                    v-model="scope.row.vSpaModel"
                    @change="handleEditPrice(scope.$index, scope.row)"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="vSelectBrand"
                label="技术选用品牌"
                align="center"
                min-width="100"
              >
                <template slot-scope="scope">
                  <el-input
                    size="small"
                    v-model="scope.row.vSelectBrand"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="vSuggestBrand"
                label="客户建议品牌"
                align="center"
                min-width="100"
              >
                <template slot-scope="scope">
                  <el-input
                    size="small"
                    v-model="scope.row.vSuggestBrand"
                  ></el-input>
                </template>
              </el-table-column>
              <!-- <el-table-column
                prop="vSpaBrand"
                label="采购报价选用品牌"
                align="center"
                min-width="100"
              >
                <template slot-scope="scope">
                  <el-input
                    size="small"
                    v-model="scope.row.vSpaBrand"
                    @change="handleEditPrice(scope.$index, scope.row)"
                  ></el-input>
                </template>
              </el-table-column> -->
              <el-table-column
                prop="vUnit"
                label="单位"
                align="center"
                min-width="60"
              >
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.vUnit"></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="supplier"
                label="供应商"
                align="center"
                min-width="60"
              >
                <template slot-scope="scope">
                  <el-input
                    size="small"
                    v-model="scope.row.supplier"
                  ></el-input>
                </template>
              </el-table-column>
              <!-- <el-table-column prop="nQty" label="单套数量" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.nQty"></el-input>
                </template>
              </el-table-column> -->
              <!-- <el-table-column prop="nSetQty" label="套数" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.nSetQty"></el-input>
                </template>
              </el-table-column> -->
              <el-table-column
                prop="nTotalQty"
                label="总数量"
                align="center"
                min-width="70"
              >
                <template slot-scope="scope">
                  <el-input
                    size="small"
                    v-model="scope.row.nTotalQty"
                    @change="priceOrqtyChange(scope.row)"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="nPrice"
                label="单价(未税)"
                align="center"
                min-width="80"
              >
                <template slot-scope="scope">
                  <el-input
                    size="small"
                    v-model="scope.row.nPrice"
                    @change="priceOrqtyChange(scope.row)"
                  ></el-input>
                </template>
              </el-table-column>
              <!-- <el-table-column
                prop="nSetPrice"
                label="单套总价(未税)"
                align="center"
              >
                <template slot-scope="scope">
                  <el-input
                    size="small"
                    v-model="scope.row.nSetPrice"
                  ></el-input>
                </template>
              </el-table-column> -->
              <el-table-column
                prop="nTotalPrice"
                label="总价(未税)"
                align="center"
                min-width="80"
              >
                <template slot-scope="scope">
                  <el-input
                    size="small"
                    v-model="scope.row.nTotalPrice"
                    disabled="true"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="nDays"
                label="货期"
                align="center"
                min-width="80"
              >
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.nDays"></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="vReplaceModel"
                label="替代品规格"
                align="center"
                min-width="80"
              >
                <template slot-scope="scope">
                  <el-input
                    size="small"
                    v-model="scope.row.vReplaceModel"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="nRePlacePrice"
                label="替代品价格"
                align="center"
                min-width="80"
              >
                <template slot-scope="scope">
                  <el-input
                    size="small"
                    v-model="scope.row.nRePlacePrice"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="vReplaceDays"
                label="替代品货期"
                align="center"
                min-width="80"
              >
                <template slot-scope="scope">
                  <el-input
                    size="small"
                    v-model="scope.row.vReplaceDays"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column align="right" min-width="150">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="primary"
                    @click="handleSuggest(scope.$index, scope.row)"
                    >建议品牌</el-button
                  >
                  <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.$index, scope.row)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- <div class="demo-drawer__footer">
          <el-button @click="cancelDrawer">取 消</el-button>
          <el-button type="primary" @click="okDrawer">确定</el-button>
        </div> -->
            <div
              class="xin-footer"
              style="position: fixed; bottom: 5px; right: 10px"
            >
              <el-button type="primary" @click="inquiry">生成询价单</el-button>
              <el-button type="primary" @click="importXJDData"
                >导入询价单结果</el-button
              >
              <el-button type="primary" @click="saveButton">保存</el-button>
              <el-button @click="cancelDrawer">取消</el-button>
            </div>
          </div>
        </div>
      </div>
    </el-drawer>

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-upload
        ref="upload"
        :limit="10"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :on-change="handleFileChange"
        :auto-upload="false"
        :multiple="true"
        :file-list="upload.fileList"
        :data="upload.uploadData"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>

        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" /> 是否覆盖已经存在的数据
        </div>
        <div class="el-upload__tip" style="color: red" slot="tip">
          提示：仅允许导入“xls”或“xlsx”格式文件！
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 询价单导入对话框 -->
    <el-dialog
      :title="uploadXJD.title"
      :visible.sync="uploadXJD.open"
      width="400px"
    >
      <el-upload
        ref="uploadXJD"
        :limit="20"
        accept=".xlsx, .xls"
        :headers="uploadXJD.headers"
        :action="uploadXJD.url"
        :disabled="uploadXJD.isUploading"
        :on-progress="handleFileUploadProgressXJD"
        :on-success="handleFileSuccessXJD"
        :on-change="handleFileChangeXJD"
        :auto-upload="false"
        :multiple="true"
        :file-list="uploadXJD.fileList"
        :data="uploadXJD.uploadData"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>

        <div class="el-upload__tip" style="color: red" slot="tip">
          提示：仅允许导入“xls”或“xlsx”格式文件！
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileFormXJD">确 定</el-button>
        <el-button @click="uploadXJD.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="suggestDialog.title"
      :visible.sync="suggestDialog.visible"
      width="50%"
      :close-on-click-modal="false"
      :modal="true"
    >
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleSuggestAdd"
            >新增</el-button
          >
        </el-col>
      </el-row>
      <el-table :data="suggestDialog.tableList" border height="400px">
        <el-table-column
          type="index"
          label="序号"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          width="120"
          prop="vSpaName"
          label="物料名称"
        >
          <template slot-scope="scope">
            <el-input size="small" v-model="scope.row.vSpaName"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          width="120"
          prop="vSpaModel"
          label="规格型号"
        >
          <template slot-scope="scope">
            <el-input size="small" v-model="scope.row.vSpaModel"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          width="120"
          prop="vSpaBrand"
          label="品牌"
        >
          <template slot-scope="scope">
            <el-input size="small" v-model="scope.row.vSpaBrand"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          width="120"
          prop="supplier"
          label="供应商"
        >
          <template slot-scope="scope">
            <el-input size="small" v-model="scope.row.supplier"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" prop="nQty" label="数量">
          <template slot-scope="scope">
            <el-input size="small" v-model="scope.row.nQty"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" prop="nPrice" label="单价">
          <template slot-scope="scope">
            <el-input size="small" v-model="scope.row.nPrice"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" prop="nDays" label="货期">
          <template slot-scope="scope">
            <el-input size="small" v-model="scope.row.nDays"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="handleSuggestDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="suggestDialogSave">确 定</el-button>
        <el-button @click="suggestDialogCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  list,
  save,
  view,
  selectSpaPriceByCondition,
  importData,
  importDataXJD,
  saveSuggestDialog,
  getSuggests,
  deleteSuggestDialog,
  exportXJ,
} from "@/api/buy/buy0002";
import { getToken } from "@/utils/auth";

export default {
  name: "Role",
  dicts: ["sys_normal_disable", "spa_spa_kind"],
  data() {
    return {
      // 表单校验
      rules: {
        vBillNo: [
          { required: true, message: "材料清单编号不能为空", trigger: "blur" },
        ],
      },
      isCover: false,
      // 遮罩层
      loading: true,
      // 遮罩层
      drawerVisibie: false,
      // 选中数组
      ids: [],
      //抽屉表格数据
      drawerList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      spaList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vBillNo: undefined,
        vProjectNo: undefined,
        vProjectDec: undefined,
        applyBy: undefined,
      },

      drawerForm: {
        id: undefined,
        vBillNo: undefined,
        vProjectNo: undefined,
        vProjectDec: undefined,
        applyBy: undefined,
      },
      formLabelWidth: "120px",
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/buy/buy0002/importData",
        fileList: [],
        uploadData: {
          updateSupport: "1",
          billID: this.form,
          list: JSON.stringify([]),
        },
      },

      // 询价结果导入
      uploadXJD: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/buy/buy0002/importXJDData",
        fileList: [],
        uploadData: {},
      },
      suggestDialog: {
        visible: false,
        title: "设置建议品牌",
        suggestBrandId: "",
        tableList: [],
        vKind: "",
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then((response) => {
        this.spaList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    handleAdd() {
      this.drawerVisibie = true;
      this.drawerList = [];
      var username = this.$store.state.user;
      this.drawerForm = { applyBy: username.name };
    },

    handleEdit(index, row) {
      this.drawerList = this.computeRate(this.drawerList);
    },

    handleEditPrice(index, row) {
      if (
        !this.checkNull(row.vSpaName) &&
        !this.checkNull(row.vSpaBrand) &&
        !this.checkNull(row.vSpaModel)
      ) {
        selectSpaPriceByCondition(row).then((response) => {
          console.log(response);
          row.nPrice = response.data.nPrice;
        });
      }
    },
    handleTable() {
      this.drawerVisibie = true;
    },
    view(row) {
      view(row).then((response) => {
        this.drawerForm = response.data.billPO;
        this.drawerList = this.computeRate(response.data.itemPOList);
        this.drawerList = this.drawerList.sort((a, b) => {
          return a.nPrice - b.nPrice;
        });
        this.drawerList.map(function (item) {
          item.nTotalPrice = item.nTotalQty * item.nPrice;
        });
        this.drawerVisibie = true;
      });
    },

    computeRate(itemPOList) {
      if (itemPOList.length <= 1) {
        return itemPOList;
      }
      //计算总金额
      let total = 0;
      itemPOList.forEach((element) => {
        // 判断价格是否为空
        if (this.checkNull(element.nPrice) || this.checkNull(element.nQty)) {
          total = total + 0;
        } else {
          let itemPrice = element.nPrice * element.nQty;
          total = total + itemPrice;
          element.itemPrice = itemPrice;
        }
      });
      let rate = 0;
      itemPOList.forEach((element) => {
        if (this.checkNull(element.nPrice) || this.checkNull(element.nQty)) {
          rate = 0;
        } else {
          let itemPrice = element.nPrice * element.nQty;
          rate = (itemPrice / total).toFixed(2);
          if (rate > 0.3) {
            element.flag = 1;
          } else {
            element.flag = 0;
          }
        }
      });
      return itemPOList;
    },
    checkNull(str) {
      if (str === "" || str === null || str === undefined || str === 0) {
        return true;
      } else {
        return false;
      }
    },
    cancelDrawer() {
      this.drawerVisibie = false;
      this.drawerList = [];
      this.drawerForm = {};
    },
    saveButton() {
      /** 修改按钮操作 */
      var _this = this;
      let flag = true;
      for (let i = 0; i < _this.drawerList.length; i++) {
        console.log("$$$$$$$$$$$$$$$$$$$$");
        console.log(_this.drawerList[i]);
        if (
          _this.drawerList[i].vSpaName === "" ||
          _this.drawerList[i].vSpaName === null ||
          _this.drawerList[i].vSpaName === undefined
        ) {
          _this.$message({
            message: "第" + (i + 1) + "行零件名称不能为空",
            type: "warning",
          });
          flag = false;
          break;
        }
        /* if (
          _this.drawerList[i].vSpaModel === "" ||
          _this.drawerList[i].vSpaModel === null ||
          _this.drawerList[i].vSpaModel === undefined
        ) {
          _this.$message({
            message: "第" + (i + 1) + "行规格型号不能为空",
            type: "warning",
          });
          flag = false;
          break;
        }*/
      }

      if (flag == true) {
        const para = {
          billPO: _this.drawerForm,
          itemPOList: _this.drawerList,
          isCover: _this.isCover,
        };
        save(para).then((response) => {
          _this.drawerList = [];
          _this.drawerForm = {};
          _this.drawerVisibie = false;
          _this.getList();
          _this.$modal.msgSuccess("保存成功");
        });
      }
    },

    // 添加行
    handleTableAdd() {
      var _this = this;

      let list = {
        vSpaName: "",
        vSpaModel: "",
        vSpaBrand: "",
        vUnit: "",
        nQty: "",
        nPrice: "",
        nDays: "",
      };

      _this.drawerList.push(list);
    },
    /**
     * 建议品牌按钮
     * @param {*} index
     * @param {*} row
     */
    async handleSuggest(index, row) {
      console.log();
      getSuggests(row).then((res) => {
        this.suggestDialog.visible = true;
        this.suggestDialog.suggestBrandId = row.iD;
        this.suggestDialog.tableList = res.data;
        this.suggestDialog.vKind = row.vKind;
      });
    },

    handleSuggestAdd() {
      this.suggestDialog.tableList.push({
        vSpaName: "",
        nMainId: this.suggestDialog.suggestBrandId,
        vKind: this.suggestDialog.vKind,
      });
    },
    handleSuggestDelete(index, row) {
      let _this = this;
      if (_this.checkNull(row.id)) {
        this.suggestDialog.tableList.splice(index, 1);
      } else {
        deleteSuggestDialog(row).then((res) => {});
        this.suggestDialog.tableList.splice(index, 1);
      }
      this.$message({ type: "success", message: "删除成功" });
    },
    suggestDialogSave() {
      let _this = this;
      saveSuggestDialog(this.suggestDialog.tableList).then((res) => {
        _this.suggestDialogCancel();
      });
    },
    suggestDialogCancel() {
      this.suggestDialog.visible = false;
      this.suggestDialog.tableList = [];
      this.suggestDialog.suggestBrandId = "";
    },
    // 删除行
    handleDelete(index, row) {
      var _this = this;
      _this.drawerList.splice(index, 1);
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "数据导入";
      this.upload.fileList = [];
      this.upload.open = true;
      this.upload.updateSupport = false;
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      console.this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.isCover = response.data.updateSupport;
      this.getDrawerList(response.data.list);
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.drawerForm.folderPath = response.data.folderPath;
      this.drawerForm.vProjectDec = response.data.vProjectDec;
    },
    handleFileChange(file, fileList) {
      this.upload.fileList = fileList;
    },
    // 提交上传文件
    submitFileForm() {
      // this.$refs.upload.submit();
      ///buy/buy0002/importData
      if (this.upload.fileList.length == 0) {
        this.$message({
          type: "error",
          message: "请先选择文件！",
          showClose: true,
        });
        return;
      }
      let formData = new FormData();
      this.upload.fileList.map((item) => {
        formData.append("files", item.raw);
      });
      formData.append("updateSupport", this.upload.updateSupport);
      formData.append("folderPath", this.drawerForm.folderPath);
      formData.append("existData", JSON.stringify(this.drawerList));
      importData(formData).then((response) => {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.isCover = this.upload.updateSupport;
        if (this.upload.updateSupport) {
          this.drawerList = [];
        }
        this.getDrawerList(response.data.list);
        this.$message({ type: "success", message: response.msg });
        this.drawerForm.folderPath = response.data.folderPath;
        this.drawerForm.vProjectDec = response.data.vProjectDec;
      });
    },
    getDrawerList(dataList) {
      dataList.map((item) => {
        console.log(item);
        debugger;
        let obj = this.drawerList.find(
          (x) =>
            x.vSpaBrand == item.vSpaBrand &&
            x.vSpaModel == item.vSpaModel &&
            x.vSpaName == item.vSpaName
        );
        if (obj) {
          obj.nSetQty += item.nSetQty;
          obj.nTotalQty += item.nTotalQty;
          obj.nTotalPrice += item.nTotalPrice;
          obj.supplier = item.supplier;
          obj.nDays = item.nDays;
        } else {
          this.drawerList.push(item);
        }
      });
    },
    cellStyle(row, column, rowIndex, columnIndex) {
      let cellStyle;
      switch (row.row.flag) {
        case 1:
          cellStyle = "color:red;background-color:yellow";
          break;
        default:
          cellStyle = "";
      }
      return cellStyle;
    },

    inquiry() {
      // this.download(
      //   "buy/buy0002/export",
      //   { userName: JSON.stringify(this.drawerList) },
      //   `询价单_${new Date().getTime()}.xlsx`
      // );
      let _this = this;
      exportXJ({ userName: JSON.stringify(this.drawerList) }).then((res) => {
        this.$download.zip("/buy/buy0002/export/" + res, "需要询价的数据.zip");
      });
    },

    exportData(row) {
      this.$download.zip("/buy/buy0002/exportData/" + row.iD, "价格表.zip");
    },

    getZip() {},

    /** 询价单结果导入按钮操作 */
    importXJDData() {
      this.uploadXJD.title = "询价单结果导入";
      this.uploadXJD.fileList = [];
      this.uploadXJD.open = true;
      this.uploadXJD.updateSupport = false;
      this.uploadXJD.uploadData.billID = this.drawerForm.id;
    },
    // 文件上传中处理
    handleFileUploadProgressXJD(event, file, fileList) {
      this.uploadXJD.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccessXJD(response, file, fileList) {
      console.this.uploadXJD.open = false;
      this.uploadXJD.isUploading = false;
      this.$refs.uploadXJD.clearFiles();
      this.drawerList = [];
      this.getDrawerList(response.data);
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
    },
    handleFileChangeXJD(file, fileList) {
      this.uploadXJD.fileList = fileList;
    },

    // 提交上传文件
    submitFileFormXJD() {
      if (this.uploadXJD.fileList.length == 0) {
        this.$message({
          type: "error",
          message: "请先选择文件！",
          showClose: true,
        });
        return;
      }
      let formData = new FormData();
      this.uploadXJD.fileList.map((item) => {
        formData.append("files", item.raw);
      });
      formData.append("billID", this.drawerForm.iD);
      importDataXJD(formData).then((response) => {
        this.uploadXJD.open = false;
        this.uploadXJD.isUploading = false;
        this.$refs.uploadXJD.clearFiles();
        this.drawerList = [];
        this.getDrawerList(response.data);
        this.drawerList = this.drawerList.sort((a, b) => {
          return a.nPrice - b.nPrice;
        });
        this.drawerList.map(function (item) {
          item.nTotalPrice = item.nTotalQty * item.nPrice;
        });
        this.$message({ type: "success", message: response.msg });
      });
    },

    priceOrqtyChange(row) {
      row.nTotalPrice = row.nPrice * row.nTotalQty;
    },
  },
};
</script>
<style>
.xin-main {
  position: relative;
  height: 100%;
  overflow: hidden;
}
.xin-content {
  overflow-y: scroll;
  box-sizing: border-box;
  overflow-x: hidden;
}
</style>
